﻿using System;
using System.Data;
using Model;
namespace Dal
{
    public class BaoMingDal
    {
        public BaoMingDal()
        {
        }
        /// <summary>
        /// 读取时间空为"1900-01-01"
        /// </summary>
        /// <param name="dt">时间</param>
        /// <returns>返回时间</returns>
        private static DateTime ReadTime(DateTime dt)
        {
            DateTime refdt = Convert.ToDateTime("1900-01-01");
            if (!string.IsNullOrEmpty(dt.ToString()) && dt.Year != 1)
            {
                refdt = dt;
            }
            return refdt;
        }
        #region Insert
        public static int Insert(BaoMing baoming)
        {
            string sql = @"Insert Into BaoMing (
				OrderNO,
				UserName,
				Sex,
				Birthday,
				PlaceOrigin,
				PlaceBirth,
				RegClass,
				IdCardNo,
				CensusClass,
				CensusAddr,
				IfInSchool,
				InSchoolInfo,
				AllergicHistory,
				SepcialDisease,
				FatherName,
				FatherWorks,
				FatherTel,
				FatherInSZ,
				FatherPaySB,
				MotherName,
				MotherWorks,
				MotherTel,
				MotherInSZ,
				MotherPaySB,
				HouseAddr,
				HouseNature,
				BuyHouseDate,
				RentHouseDate,
				Planning,
				ChildCounts,
				ChildIndex,
				CaseType,
				SignName,
				SignDate,
				Notes,
				AddTime,
				Photos,
				Status,
				ClassNum,
				ClassNo,
				CheckInfos,
				UserPwd,
				Email,
				UserName2,
				UserId,
				NurserySchool,
				FatherCardNo,
				MotherCardNo,
				HouseRegionID,
				HouseGroupID,
				HouseNumber,				HouseNumber1,				HouseNumber2,				HouseArea,				NoteNo,
                CensusProvince,
                CensusCity,
                CensusArea,
                CensusPoliceStation,
				MotherPayJZZ,
				FatherPayJZZ
				) values('{0}','{1}',{2},'{3}','{4}','{5}',{6},'{7}',{8},'{9}',{10},'{11}','{12}','{13}','{14}','{15}','{16}',{17},{18},'{19}','{20}','{21}',{22},{23},'{24}',{25},'{26}','{27}',{28},{29},{30},'{31}','{32}','{33}','{34}','{35}','{36}',{37},'{38}','{39}','{40}','{41}','{42}','{43}',{44},{45},'{46}','{47}',{48},{49},{50},{51},{52},'{53}','{54}','{55}','{56}','{57}','{58}',{59},{60})";
            sql = string.Format(sql,
                SqlHelper.String(baoming.OrderNO),
                SqlHelper.String(baoming.UserName),
                baoming.Sex,
                ReadTime(baoming.Birthday),
                SqlHelper.String(baoming.PlaceOrigin),
                SqlHelper.String(baoming.PlaceBirth),
                baoming.RegClass,
                SqlHelper.String(baoming.IdCardNo),
                baoming.CensusClass,
                SqlHelper.String(baoming.CensusAddr),
                baoming.IfInSchool,
                SqlHelper.String(baoming.InSchoolInfo),
                SqlHelper.String(baoming.AllergicHistory),
                SqlHelper.String(baoming.SepcialDisease),
                SqlHelper.String(baoming.FatherName),
                SqlHelper.String(baoming.FatherWorks),
                SqlHelper.String(baoming.FatherTel),
                baoming.FatherInSZ,
                baoming.FatherPaySB,
                SqlHelper.String(baoming.MotherName),
                SqlHelper.String(baoming.MotherWorks),
                SqlHelper.String(baoming.MotherTel),
                baoming.MotherInSZ,
                baoming.MotherPaySB,
                SqlHelper.String(baoming.HouseAddr),
                baoming.HouseNature,
                ReadTime(baoming.BuyHouseDate),
                ReadTime(baoming.RentHouseDate),
                baoming.Planning,
                baoming.ChildCounts,
                baoming.ChildIndex,
                SqlHelper.String(baoming.CaseType),
                SqlHelper.String(baoming.SignName),
                ReadTime(baoming.SignDate),
                SqlHelper.String(baoming.Notes),
                ReadTime(baoming.AddTime),
                SqlHelper.String(baoming.Photos),
                baoming.Status,
                SqlHelper.String(baoming.ClassNum),
                SqlHelper.String(baoming.ClassNo),
                SqlHelper.String(baoming.CheckInfos),
                SqlHelper.String(baoming.UserPwd),
                SqlHelper.String(baoming.Email),
                SqlHelper.String(baoming.UserName2),
                baoming.UserId,
                baoming.NurserySchool,
                SqlHelper.String(baoming.FatherCardNo),
                SqlHelper.String(baoming.MotherCardNo),
                baoming.HouseRegionID,
                baoming.HouseGroupID,
                baoming.HouseNumberID,
                baoming.HouseNumber1,
                baoming.HouseNumber2,
                baoming.HouseArea,
                baoming.NoteNo,
                baoming.CensusProvince,
                baoming.CensusCity,
                baoming.CensusArea,
                baoming.CensusPoliceStation,
                baoming.MotherPayJZZ,
                baoming.FatherPayJZZ
            );
            return SqlHelper.ExecSql(sql);
        }
        #endregion

        #region Update
        public static int Update(BaoMing baoming)
        {
            string sql = @"Update BaoMing set 				OrderNO='{0}',				UserName='{1}',				Sex={2},				Birthday='{3}',				PlaceOrigin='{4}',				PlaceBirth='{5}',				RegClass={6},				IdCardNo='{7}',				CensusClass={8},				CensusAddr='{9}',				IfInSchool={10},				InSchoolInfo='{11}',				AllergicHistory='{12}',				SepcialDisease='{13}',				FatherName='{14}',				FatherWorks='{15}',				FatherTel='{16}',				FatherInSZ={17},				FatherPaySB={18},				MotherName='{19}',				MotherWorks='{20}',				MotherTel='{21}',				MotherInSZ={22},				MotherPaySB={23},				HouseAddr='{24}',				HouseNature={25},				BuyHouseDate='{26}',				RentHouseDate='{27}',				Planning={28},				ChildCounts={29},				ChildIndex={30},				CaseType='{31}',				SignName='{32}',				SignDate='{33}',				Notes='{34}',				AddTime='{35}',				Photos='{36}',				Status={37},				ClassNum='{38}',				ClassNo='{39}',				CheckInfos='{40}',				UserPwd='{41}',				Email='{42}',				UserName2='{43}',				UserId={44},				NurserySchool={45},				FatherCardNo='{46}',				MotherCardNo='{47}',				HouseRegionID='{49}',				HouseGroupID='{50}',				HouseNumber='{51}',				HouseNumber1='{52}',				HouseNumber2='{53}',				HouseArea='{54}',				NoteNo='{55}',
                CensusProvince='{56}',
                CensusCity='{57}',
                CensusArea='{58}',
                CensusPoliceStation='{59}',				MotherPayJZZ={60},				FatherPayJZZ={61}				where				Id={48}";
            sql = string.Format(sql,
                SqlHelper.String(baoming.OrderNO),
                SqlHelper.String(baoming.UserName),
                baoming.Sex,
                ReadTime(baoming.Birthday),
                SqlHelper.String(baoming.PlaceOrigin),
                SqlHelper.String(baoming.PlaceBirth),
                baoming.RegClass,
                SqlHelper.String(baoming.IdCardNo),
                baoming.CensusClass,
                SqlHelper.String(baoming.CensusAddr),
                baoming.IfInSchool,
                SqlHelper.String(baoming.InSchoolInfo),
                SqlHelper.String(baoming.AllergicHistory),
                SqlHelper.String(baoming.SepcialDisease),
                SqlHelper.String(baoming.FatherName),
                SqlHelper.String(baoming.FatherWorks),
                SqlHelper.String(baoming.FatherTel),
                baoming.FatherInSZ,
                baoming.FatherPaySB,
                SqlHelper.String(baoming.MotherName),
                SqlHelper.String(baoming.MotherWorks),
                SqlHelper.String(baoming.MotherTel),
                baoming.MotherInSZ,
                baoming.MotherPaySB,
                SqlHelper.String(baoming.HouseAddr),
                baoming.HouseNature,
                ReadTime(baoming.BuyHouseDate),
                ReadTime(baoming.RentHouseDate),
                baoming.Planning,
                baoming.ChildCounts,
                baoming.ChildIndex,
                SqlHelper.String(baoming.CaseType),
                SqlHelper.String(baoming.SignName),
                ReadTime(baoming.SignDate),
                SqlHelper.String(baoming.Notes),
                ReadTime(baoming.AddTime),
                SqlHelper.String(baoming.Photos),
                baoming.Status,
                SqlHelper.String(baoming.ClassNum),
                SqlHelper.String(baoming.ClassNo),
                SqlHelper.String(baoming.CheckInfos),
                SqlHelper.String(baoming.UserPwd),
                SqlHelper.String(baoming.Email),
                SqlHelper.String(baoming.UserName2),
                baoming.UserId,
                baoming.NurserySchool,
                SqlHelper.String(baoming.FatherCardNo),
                SqlHelper.String(baoming.MotherCardNo),
                baoming.Id,
                baoming.HouseRegionID,
                baoming.HouseGroupID,
                baoming.HouseNumberID,
                baoming.HouseNumber1,
                baoming.HouseNumber2,
                baoming.HouseArea,
                baoming.NoteNo,
                baoming.CensusProvince,
                baoming.CensusCity,
                baoming.CensusArea,
                baoming.CensusPoliceStation,
                baoming.MotherPayJZZ,
                baoming.FatherPayJZZ);
            return SqlHelper.ExecSql(sql);
        }
        #endregion



        #region Delete
        public static int Delete(Int32 Id)
        {

            string sql = @"Delete from BaoMing where 
					Id= {0}";
            sql = string.Format(sql,
                    Id);
            return SqlHelper.ExecSql(sql);
        }
        #endregion

        #region Select

        public static DataSet Select()
        {
            string sql = "select a.* from BaoMing a ";
            return SqlHelper.Select(sql);
        }

        public static BaoMing Select(Int32 Id)
        {
            BaoMing baoming = null;
            string sql = @"select a.* from BaoMing a  where
					a.Id= {0}";
            sql = string.Format(sql,
                    Id);
            DataSet ds = SqlHelper.Select(sql);
            if (SqlHelper.HasRow(ds))
                baoming = new BaoMing(ds.Tables[0].Rows[0]);
            return baoming;
        }

        public static DataSet Select(string where)
        {
            if (where.Trim().ToLower().StartsWith("where"))
                where = where.Substring(5);
            if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
                where = " and " + where;
            string sql = "select a.* from BaoMing a  where 1=1 " + where;
            return SqlHelper.Select(sql);
        }

        public static DataSet SelectForList(string where, int pageIndex, int pageSize, bool bCount, out int count)
        {
            count = 0;
            if (where.Trim().ToLower().StartsWith("where"))
                where = where.Substring(5);
            if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
                where = " and " + where;
            string sql = "select a.* from BaoMing a where 1=1 " + where;

            return SqlHelper.Select(sql, pageSize, pageIndex, bCount, ref count);
        }
        public static DataSet Select(string where, int pageIndex, int pageSize, bool bCount, out int count)
        {
            count = 0;
            if (where.Trim().ToLower().StartsWith("where"))
                where = where.Substring(5);
            if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
                where = " and " + where;
            string sql = "select a.* from BaoMing a  where 1=1 " + where;
            return SqlHelper.Select(sql, pageSize, pageIndex, bCount, ref count);
        }
        #endregion
    }
}